﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;


namespace BasicMachine
{
    public partial class EasyMail : Component
    {
        // All the fields that are needed
        private string mMailFrom;
        private string mMailTo;
        private string mMailSubject;
        private string mMailBody;
        private string mSMTPServer;
        private int mSMTPPort;
        private string mSMTPUsername;
        private string mSMTPPassword;
        private bool mSMTPSSL;
        private MailMessage MailObject;

        // Properties
        public string MailFrom
        {
            set { mMailFrom = value; }
            get { return mMailFrom; }
        }
        public string MailTo
        {
            set { mMailTo = value; }
            get { return mMailTo; }
        }
        public string MailSubject
        {
            set { mMailSubject = value; }
            get { return mMailSubject; }
        }
        public string MailBody
        {
            set { mMailBody = value; }
            get { return mMailBody; }
        }
        public string SMTPServer
        {
            set { mSMTPServer = value; }
            get { return mSMTPServer; }
        }
        public int SMTPPort
        {
            set { mSMTPPort = value; }
            get { return mSMTPPort; }
        }
        public string SMTPUsername
        {
            set { mSMTPUsername = value; }
            get { return mSMTPUsername; }
        }
        public string SMTPPassword
        {
            set { mSMTPPassword = value; }
            get { return mSMTPPassword; }
        }
        public Boolean SMTPSSL
        {
            set { mSMTPSSL = value; }
            get { return mSMTPSSL; }
        }


        // Functions
        public Boolean Send()
        {
            // build the email message
            Boolean bTemp;

            try
            {
                MailMessage Email = new MailMessage();
                MailAddress MailFrom =
                  new MailAddress(mMailFrom, mMailFrom);
                Email.From = MailFrom;
                Email.To.Add(mMailTo);

                Email.Subject = mMailSubject;
                Email.Body = mMailBody;

                // Smtp Client
                SmtpClient SmtpMail =
                 new SmtpClient(mSMTPServer, mSMTPPort);
                SmtpMail.Credentials =
                 new NetworkCredential(mSMTPUsername, mSMTPPassword);
                SmtpMail.EnableSsl = mSMTPSSL;

                bTemp = true;

                SmtpMail.Send(Email);
                return true;
            }
            catch (SmtpFailedRecipientsException ex)
            {
                MessageBox.Show("The message was not sent!!!");
                bTemp = false;
            }
            return bTemp;
        }

        // Constructor
        public EasyMail()
        {
            InitializeComponent();

            MailObject = new MailMessage();
            mMailFrom = "";
            mMailTo = "";
            mMailSubject = "";
            mMailBody = "";
            mSMTPServer = "";
            mSMTPPort = 25;
            mSMTPUsername = "";
            mSMTPPassword = "";
            mSMTPSSL = false;
        }

        public EasyMail(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }
    }

}
